import time

from integration.alarmsHandle.alarms_focus import Alarms_Focus
from integration.serce.serce_focus import Serce_Focus
from integration.spark.spark import Spark
from log_time_sleeps.logs import Encryption
from log_time_sleeps.sql import SQLiteManager
import multiprocessing
from apscheduler.schedulers.background import BackgroundScheduler



class Main:
    def __init__(self):
        self.sqlite()
        self.time_time()
        ##改时间看下面这个
        # self.startTime='2024-12-5 20:00:00'

    def sqlite(self):
        self.db_manager = SQLiteManager()

    def time_time(self):
        self.__dict__.update(Encryption().time_cha().__dict__)

    def alarmsHandle_login(self,select):
        zhi = 'alarmsHandle'  ##智能网管
        url_list = self.db_manager.select_cookies(zhi)
        for urls_list in url_list:
            Alarms_Focus(urls_list, self.__dict__).select(select=select)

    def spack(self):
        zhi = 'spark'  ##大数据
        url_list = self.db_manager.select_cookies(zhi)
        for urls_list in url_list:
            Spark(urls_list, self.my_logg)
    def serce(self,select):
        zhi='serce'
        url_list = self.db_manager.select_cookies(zhi)
        for urls_list in url_list:
            Serce_Focus(urls_list, self.__dict__).select(select=select)

    def run_tasks(self):
        try:
            # 创建进程池
            pool = multiprocessing.Pool(3)
            # 创建后台调度器
            scheduler = BackgroundScheduler()
            # scheduler.add_job(self.spack, 'interval', seconds=30)  # 每20秒运行一次
            # scheduler.add_job(self.serce(2), 'interval', minutes=30) # 每30分钟运行一次
            scheduler.add_job(self.spack, 'cron', hour='4,6,9')
            # 开始调度器
            scheduler.start()
            pool.apply_async(self.spack)  # 添加任务到进程池
            # scheduler.add_job(self.serce(2), 'interval', minutes=30)
            pool.close()
            pool.join()
            # 保持程序运行，以便调度器可以继续执行定时任务
            while True:
                time.sleep(1)
        except (KeyboardInterrupt, SystemExit):
            # 优雅地关闭调度器
            scheduler.shutdown()
            # 关闭进程池
            pool.terminate()